package com.panfeng.xcloud.component.ocr;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.cloudauth.model.v20180916.GetMaterialsRequest;
import com.aliyuncs.cloudauth.model.v20180916.GetMaterialsResponse;
import com.aliyuncs.cloudauth.model.v20180916.GetVerifyTokenRequest;
import com.aliyuncs.cloudauth.model.v20180916.GetVerifyTokenResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.panfeng.xcloud.component.ocr.config.AppOcrVerPropConfig;
import com.panfeng.xcloud.component.ocr.model.UserOcrAuthMaterialsInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 阿里云ocr服务
 *
 * @author xiaobo
 * @date 2018-02-07
 */
@Slf4j
@Component
public class OcrMaker {

    @Autowired
    private AppOcrVerPropConfig appOcrVerPropConfig;

    /**
     * 在进行ocr实名认证前需要获取ocr token
     *
     * @param ticketId
     * @return
     */
    public String getOcrToken(String ticketId){
        log.info(">>> 传递的认证ticketId：{} <<<",ticketId);
        DefaultProfile profile = DefaultProfile.getProfile(
                appOcrVerPropConfig.getRegionId(),
                appOcrVerPropConfig.getAccessKeyId(),
                appOcrVerPropConfig.getAccessKeySecret());
        IAcsClient client = new DefaultAcsClient(profile);
        String biz = appOcrVerPropConfig.getBiz();
        String token = "";
        GetVerifyTokenRequest getVerifyTokenRequest = new GetVerifyTokenRequest();
        getVerifyTokenRequest.setBiz(biz);
        getVerifyTokenRequest.setActionName("GetVerifyToken");
        getVerifyTokenRequest.setTicketId(ticketId);
        try {
            GetVerifyTokenResponse response = client.getAcsResponse(getVerifyTokenRequest);
            //token默认30分钟时效，每次发起认证时都必须实时获取
            token = response.getData().getVerifyToken().getToken();
        } catch (ServerException e) {
            log.info(">>> 获取ocr token失败");
            e.printStackTrace();
        } catch (ClientException e) {
            log.info(">>> 获取ocr token失败");
            e.printStackTrace();
        }
        return token;
    }

    /**
     * 实名认证之后获取用户实名认证资料
     *
     * @param ticketId
     * @return
     */
    public UserOcrAuthMaterialsInfo getOcrUserMaterials(String ticketId){
        UserOcrAuthMaterialsInfo userOcrAuthMaterialsInfo = new UserOcrAuthMaterialsInfo();
        log.info(">>> 实名认证之后获取实名认证资料传递的认证ticketId：{} <<<",ticketId);
        DefaultProfile profile = DefaultProfile.getProfile(
                appOcrVerPropConfig.getRegionId(),
                appOcrVerPropConfig.getAccessKeyId(),
                appOcrVerPropConfig.getAccessKeySecret());
        IAcsClient client = new DefaultAcsClient(profile);
        String biz = appOcrVerPropConfig.getBiz();
        GetMaterialsRequest getMaterialsRequest = new GetMaterialsRequest();
        getMaterialsRequest.setBiz(biz);
        getMaterialsRequest.setActionName("GetMaterials");
        getMaterialsRequest.setTicketId(ticketId);
        try {
            GetMaterialsResponse response = client.getAcsResponse(getMaterialsRequest);
            GetMaterialsResponse.Data data = response.getData();
            if(null != data){
                userOcrAuthMaterialsInfo.setAddress(data.getAddress());
                userOcrAuthMaterialsInfo.setIdCardExpiry(data.getIdCardExpiry());
                userOcrAuthMaterialsInfo.setIdCardStartDate(data.getIdCardStartDate());
                userOcrAuthMaterialsInfo.setIdCardType(data.getIdCardType());
                userOcrAuthMaterialsInfo.setIdentificationNumber(data.getIdentificationNumber());
                userOcrAuthMaterialsInfo.setName(data.getName());
                userOcrAuthMaterialsInfo.setSex(data.getSex());
            }
        } catch (ServerException e) {
            log.info(">>> 获取实名认证信息失败");
            e.printStackTrace();
        } catch (ClientException e) {
            log.info(">>> 获取实名认证信息失败");
            e.printStackTrace();
        }
        return userOcrAuthMaterialsInfo;
    }

}
